package com.gitee.jktantan.datasource.monitor.analyze.p6spy.appends;


import com.gitee.jktantan.datasource.monitor.analyze.p6spy.QueryCollector;
import com.gitee.jktantan.datasource.monitor.analyze.p6spy.QueryEntry;
import com.gitee.jktantan.datasource.monitor.toolkit.SpringUtil;
import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.Slf4JLogger;

/**
 * 同时输出Log
 */
public class AnalyzeWithSlf4jLogger extends Slf4JLogger {
    @Override
    public void logText(String s) {
        QueryCollector queryCollector = SpringUtil.getBean(QueryCollector.class);
        queryCollector.collectQueryStatement(s);
    }

    @Override
    public void logSQL(int connectionId, String now, long elapsed, Category category, String prepared, String sql, String url) {
        super.logSQL(connectionId, now, elapsed, category, prepared, sql, url);
        QueryEntry queryEntry = new QueryEntry();
        queryEntry.setCategory(category.toString());
        queryEntry.setConnectionID(String.valueOf(connectionId));
        queryEntry.setElapsedTime(elapsed);
        queryEntry.setTime(Long.parseLong(now));
        queryEntry.setPreparedSQL(prepared);
        queryEntry.setSqlStatement(sql);
        queryEntry.setUrl(url);
        QueryCollector queryCollector = SpringUtil.getBean(QueryCollector.class);
        queryCollector.collectQuery(queryEntry);
    }
}
